package top.putileaf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.putileaf.pojo.Comment;
import top.putileaf.pojo.Result;
import top.putileaf.service.CommentService;
import top.putileaf.utils.ThreadLocalUtil;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RequestMapping("/comment")
@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;
    @PostMapping("/add")
    public Result<String> add(@RequestBody Comment content){
        System.out.println("content的值:"+content);
        //获取用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId =(Integer) map.get("id");
        content.setCreateUser(userId);
        content.setCreateTime(LocalDateTime.now());
        commentService.add(content);
        return Result.successT("评论成功");
    }
    @GetMapping("/list")
    public Result<List<Comment>> list(@RequestParam Integer articleId){
        List<Comment> map = commentService.list(articleId);
        return Result.success(map);
    }

}
